//
//  UserInfoController.m
//  SleepingElf
//
//  Created by karong on 2023/10/27.
//

#import "UserInfoController.h"
#import "SETextFieldAlterController.h"
#import "UserRequest.h"
#import "DeleteUserAlter.h"

@interface UserInfoController ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UIButton *userImageBtn;
@property (weak, nonatomic) IBOutlet UIButton *userNameBtn;
@property (weak, nonatomic) IBOutlet UIButton *userIdBtn;

@property (nonatomic, strong) UIImagePickerController * imagePickerController;

@end

@implementation UserInfoController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self layoutView];
}

- (void)layoutView {
    [self.userImageBtn sd_setImageWithURL:[NSURL URLWithString:appUserCenter.avatar] forState:(UIControlStateNormal) placeholderImage:SEUserPlaceholderImage];
    [self.userNameBtn setTitle:appUserCenter.nickname forState:(UIControlStateNormal)];
    [self.userIdBtn setTitle:SESF(@"%ld", (long)appUserCenter.id) forState:(UIControlStateNormal)];
}

- (void)editUserRequestWithNickname:(NSString *)nickname {
    [SEToast loading];
    [UserRequest editUserInfoWithNickname:nickname success:^{
        appUserCenter.nickname = nickname;
        [SEToast dismiss];
        [self layoutView];
    } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {
        
    }];
}

- (void)delUserRequest {
    [UserRequest delUserWithSuccess:^{
        [SEToast simple:WSLocalized(@"注销成功")];
        [self.navigationController popToRootViewControllerAnimated:YES];
    } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {
        
    }];
}

- (IBAction)userImageAction:(id)sender {
//    UIViewController *setUserAvatarVC = [[CTMediator sharedInstance]ct_showUserAvatarWithSelectAvatarHandle:^(NSInteger integer) {
//        if (integer == 1) {
            //相冊
            self.imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            [self presentViewController:self.imagePickerController animated:YES completion:nil];
//        }
//        else if (integer == 2) {
//            //拍照
//            self.imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
//            [self presentViewController:self.imagePickerController animated:YES completion:nil];
//        }
//    }];
//    setUserAvatarVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
//    setUserAvatarVC.modalPresentationStyle = UIModalPresentationCustom;
//    [self presentViewController:setUserAvatarVC animated:YES completion:nil];
}

- (IBAction)userNameAction:(id)sender {
    WeakSelf
    UIViewController * target = [[CTMediator sharedInstance]ct_showTFAlterWithTitle:@"" content:appUserCenter.nickname placeholderStr:WSLocalized(@"请输入昵称") maxWordCount:10 confirm:@"" confirmHandle:^(NSString *string) {
        [weakSelf editUserRequestWithNickname:string];
    } cancel:@""];
    target.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    target.modalPresentationStyle = UIModalPresentationCustom;
    [self presentViewController:target animated:YES completion:nil];
}

- (IBAction)loginOutAction:(id)sender {
    [appUserCenter logOut];
    [SEToast simple:WSLocalized(@"退出成功")];
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (IBAction)delUserAction:(id)sender {
    DeleteUserAlter *targetVC = [self.storyboard instantiateViewControllerWithIdentifier:@"DeleteUserAlter"];
    targetVC.confirmHandle = ^{
        [self delUserRequest];
    };
    targetVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    targetVC.modalPresentationStyle = UIModalPresentationCustom;
    [self presentViewController:targetVC animated:YES completion:nil];
}

#pragma mark UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey, id> *)info {
    [picker dismissViewControllerAnimated:YES completion:^{
        UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
        [self.userImageBtn setImage:image forState:(UIControlStateNormal)];
        // 提交頭像
        [UserRequest uploadImage:image success:^(NSString *string) {
            appUserCenter.avatar = string;
        } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {

        }];
    }];
    
}

- (UIImagePickerController *)imagePickerController {
    if (!_imagePickerController) {
        _imagePickerController = [[UIImagePickerController alloc] init];
        _imagePickerController.delegate = self;
        _imagePickerController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        _imagePickerController.allowsEditing = YES;
    }
    return _imagePickerController;
}

@end
